package stool.juc.airconditioner;

import java.util.Objects;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  题目：现在两个线程，可以操作初始值为零的一个变量，
 *  实现一个线程对该变量加1，一个线程对该变量-1，
 *  实现交替，来10轮，变量初始值为0.
 *  1. 高内聚低耦合前提下，线程操作资源类
 *  2. 判断/干活/通知
 *  3. 防止虚假唤醒(判断只能用while，不能用if)
 *  知识小总结：多线程编程套路+while判断+新版写法
 * @author Felix YF Dong
 * @Description
 * @Date 2023/2/17
 */
public class AirconditionerRunner {

    public static void main(String[] args) {
        AirconditionerLocker airconditioner = new AirconditionerLocker();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airconditioner.increment();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airconditioner.decrement();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airconditioner.increment();
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airconditioner.decrement();
            }
        }, "D").start();
    }
}

class Airconditioner {
    private int number = 0;

    public synchronized void increment() {
        // 防止虚假唤醒(判断只能用while，不能用if)
        while (!Objects.equals(number, 0)) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        number ++;
        System.out.println(Thread.currentThread().getName() + "~" + number);
        this.notify();
    }

    public synchronized void decrement() {
        while (Objects.equals(number, 0)) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        number --;
        System.out.println(Thread.currentThread().getName() + "~" + number);
        this.notify();
    }
}

class AirconditionerLocker {

    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() {
        lock.lock();
        try {
            while (!Objects.equals(number, 0)) {
                condition.await();
            }
            number ++;
            System.out.println(Thread.currentThread().getName() + "~" + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void decrement() {
        lock.lock();
        try {
            while (Objects.equals(number, 0)) {
                condition.await();
            }
            number --;
            System.out.println(Thread.currentThread().getName() + "~" + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }
}






